<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
      <el-form-item label="门店名称" prop="storeId">
        <el-select v-model="dataForm.storeId" placeholder="请选择门店名称" style="width:100%">
          <el-option v-for="item in StoreList" :key="item.id" :label="item.storeName" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="活动名称" prop="activityId">
        <el-select v-model="dataForm.activityId" placeholder="请选择项目名称" style="width:100%">
          <el-option v-for="item in dataList" :key="item.id" :label="item.title" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="代金券名称" prop="couponName">
        <el-input v-model="dataForm.couponName" placeholder="代金券名称" />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input v-model="dataForm.unit" placeholder="单位" />
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input v-model="dataForm.price" placeholder="价格" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        visible: false,
        StoreList: [], // 门店字典
        ItemsList: [], // 项目字典
        dataList: [], // 活动字典
        pageIndex: 1, // 当前页
        pageSize: 10000, // 每页显示条数
        totalPage: 0, // 总条数
        dataForm: {
          id: 0,
          storeId: '',
          activityId: '',
          couponName: '',
          unit: '',
          storeName: '',
          activityName: '',
          price: '',
          // createTime: ''
        },
        dataRule: {
          storeId: [
            { required: true, message: '门店id不能为空', trigger: 'blur' }
          ],
          activityId: [
            { required: true, message: '活动id不能为空', trigger: 'blur' }
          ],
          couponName: [
            { required: true, message: '代金券名称不能为空', trigger: 'blur' }
          ],
          unit: [
            { required: true, message: '单位不能为空', trigger: 'blur' }
          ],
          storeName: [
            { required: true, message: '门店名称不能为空', trigger: 'blur' }
          ],
          activityName: [
            { required: true, message: '活动名称不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '价格不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    mounted() {
      this.getStoreList()
      this.getDataList();
      // if (this.$route.query.id) {
      //   this.dataForm.id = this.$route.query.id;
      //   this.init(this.$route.query.id);
      // }
    },
    methods: {
      // 获取门店名称 ID
      async getStoreList() {
        let {
          data
        } = await this.$http({
          url: this.$http.adornUrl(`/activity/store/listStore`),
          method: 'get',
          params: this.$http.adornParams()
        })
        if (data && data.code === 0) {
          this.StoreList = data.StoreList
        }
      },
      // 获取项目名称 ID
      async changeStoreList(row) {
        let {
          data
        } = await this.$http({
          url: this.$http.adornUrl(`/activity/items/listItems/${row}`),
          method: 'get',
          params: this.$http.adornParams()
        })
        if (data && data.code === 0) {
          this.ItemsList = data.itemsList
        }
      },
      // 获取数据列表
      getDataList() {
        this.$http({
          url: this.$http.adornUrl("/activity/activity/list"),
          method: "get",
          params: this.$http.adornParams({
            page: this.pageIndex,
            limit: this.pageSize,
            key: '',
          }),
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list;
          } else {
            this.dataList = [];
          }
        });
      },
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/activity/coupon/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.dataForm.storeId = data.coupon.storeId
                this.dataForm.activityId = data.coupon.activityId
                this.dataForm.couponName = data.coupon.couponName
                this.dataForm.unit = data.coupon.unit
                this.dataForm.price = data.coupon.price
                this.dataForm.createTime = data.coupon.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/activity/coupon/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'storeId': this.dataForm.storeId,
                'activityId': this.dataForm.activityId,
                'couponName': this.dataForm.couponName,
                'unit': this.dataForm.unit,
                'storeName': this.StoreList.filter(v => v.id === this.dataForm.storeId)[0].storeName,
                'activityName': this.dataList.filter(v => v.id === this.dataForm.activityId)[0].title,
                'price': this.dataForm.price,
                // 'createTime': this.dataForm.createTime
              })
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
